%%
%第三问代码
clc
clear
syms x y
x_tou3 = zeros(2,900);
qiyu_zuobiao1 = zeros(224,900);
qiyu_zuobiao2 = zeros(224,900);
jieguo = 0;
for i = 0.55:-0.01:0.3
    changdu = 3.41-2*0.275;
    banjing_xin = 16*i;
    jiaodu_juti = 1/banjing_xin;
    %圆的方程
    jiaodu = (2*pi*4.5)/i;
    ma = jiaodu*i*cos(jiaodu);
    mb = jiaodu*i*sin(jiaodu);
    %螺线方程
    for j = 0:900
        jiaodu_fangcheng = (16*2*pi)-jiaodu_juti*j;
        x_tou3(1,j+1) = i*jiaodu_fangcheng/(2*pi)*cos(jiaodu_fangcheng);
        x_tou3(2,j+1) = i*jiaodu_fangcheng/(2*pi)*sin(jiaodu_fangcheng);
        leiji_theta = jiaodu_fangcheng;
        bianjingwenti = i*jiaodu_fangcheng/(2*pi);
        %其余位置坐标
        for m = 2:223
            if m ==2
                tou_qiyu =2.86; 
            else
                tou_qiyu = 1.65; 
            end
             bianhua_heta = tou_qiyu / bianjingwenti;
             leiji_theta = leiji_theta + bianhua_heta;
             bianjingwenti = 0.55*leiji_theta/(2 * pi);
             %每一个的位置
             qiyu_zuobiao1(m, j+1) = bianjingwenti * cos(leiji_theta);
             qiyu_zuobiao2(m, j+1) = bianjingwenti * sin(leiji_theta);
        end 
    end
    %计算是否发生碰撞
    for t =1:900
    %每一个的具体的值
    for a =1:223-1
        for b =2:223-1     
            P1 = [qiyu_zuobiao1(a,t), qiyu_zuobiao2(a,t)];
            P2 = [qiyu_zuobiao1(a+1,t), qiyu_zuobiao2(a+1,t)];   
            Q1 = [qiyu_zuobiao1(b,t), qiyu_zuobiao2(b,t)];
            Q2 = [qiyu_zuobiao1(b+1,t), qiyu_zuobiao2(b+1,t)];   
            % 判断是否相交
            jitiqingkuang = shifouxiangjiao(P1, P2, Q1, Q2);
            d1 = sqrt(Q1(1)^2+Q1(2)^2);
            if (jitiqingkuang)&&(d1==4.5)
                if d1==4.5
                    disp('相切');
                    jieguo = i;
                    break;
                else                                 
                    disp(['Segments intersect at time',num2str(t),'between points',num2str(a),'and',num2str(b)]);
                    break;
                end
            end
        end
    end
    end
end
disp('最终结果：',i)
function [jitiqingkuang] = shifouxiangjiao(P1, P2, Q1, Q2)
    function d = direction(p1, p2, q)
        d = (p2(1) - p1(1)) * (q(2) - p1(2)) - (p2(2) - p1(2)) * (q(1) - p1(1));
    end   
    jitiqingkuang1Q1 = direction(P2, P1, Q1) * direction(P2, P1, Q2) < 0;
    jitiqingkuang1Q2 = direction(P2, Q1, Q2) * direction(P2, Q1, P1) < 0;
    jitiqingkuangP1 = direction(Q2, Q1, P1) * direction(Q2, Q1, P2) < 0;
    jitiqingkuangP2 = direction(Q2, P1, P2) * direction(Q2, P1, Q1) < 0;
    jitiqingkuang = (jitiqingkuang1Q1 && jitiqingkuang1Q2) || (jitiqingkuangP1 && jitiqingkuangP2);
end






